package com.spring.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.spring.gowhere.R;
import com.spring.httputils.RequestParams;
import com.spring.httputils.RestClient;
import com.spring.httputils.TextHttpResponseHandler;
import com.spring.model.ReturnAccountInfo;
import com.spring.model.ReturnBagsInfo;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的账户
 * 
 * @author Administrator
 * 
 */
public class MyAccountActivity extends BaseActivity {
	RestClient restClient = null;
	RelativeLayout airorder, airorder2;
	private String username, logintype;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.myaccount);
		SharedPreferences userInfo = getSharedPreferences("user_info", 0);
		username = userInfo.getString("username", "");
		logintype = userInfo.getString("logintype", "");
		restClient = new RestClient(MyAccountActivity.this);
		airorder = (RelativeLayout) findViewById(R.id.airorder);
		ImageButton imageButton = (ImageButton) findViewById(R.id.title_left_ib);
		imageButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
		TextView textView = (TextView) findViewById(R.id.title_tv);
		textView.setText("我的账户");
		airorder.setOnClickListener(new OnClickListener() { // 我的钱包

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(MyAccountActivity.this, MyBagsInfoActivity.class);
				startActivity(intent);
			}
		});
		airorder2 = (RelativeLayout) findViewById(R.id.airorder2);
		airorder2.setOnClickListener(new OnClickListener() { // 我的账户

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Intent intent = new Intent(MyAccountActivity.this,QueryAccountActivity.class);
						startActivity(intent);
					}
				});

	}

	private void sendHttpDate(String url, final int type) {
		RequestParams requestparams = new RequestParams(url, "POST");
		requestparams.put("LoginAccount", "no1");
		requestparams.put("LoginType", "2");
		TextHttpResponseHandler textHttpResponseHandler = new TextHttpResponseHandler() {

			@Override
			public void onSuccess(int statusCode, Header[] headers,
					String responseString) {
				// TODO Auto-generated method stub
				Log.e("TAG", "responseString---" + responseString);
				try {
					AnalysisData(responseString, type);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				// TODO Auto-generated method stub
				Log.e("TAG", "responseString---" + responseString);

			}
		};
		try {
			restClient.post(url, requestparams, textHttpResponseHandler);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * json数据解析
	 * 
	 * @param strResult
	 * @throws JSONException
	 */
	private void AnalysisData(String strResult, int type) throws JSONException {
		/**
		 * 这里需要分析服务器回传的json格式数据，
		 */

		if (type == 1) { // 我的钱包
			JSONObject jo = new JSONObject(strResult);
			JSONArray jsonArray = (JSONArray) jo.get("BagsMoney");
			ReturnBagsInfo bagsinfo = new ReturnBagsInfo();
			List<String> listinfo = new ArrayList<String>();
			for (int i = 0; i < jsonArray.length(); ++i) {
				listinfo.add((String) jsonArray.get(i));
			}
			bagsinfo.setBagsMoney(listinfo);
			
			
			bagsinfo.setConsumeMoney(jo.getDouble("ConsumeMoney"));
			bagsinfo.setConsumeDate(jo.getString("ConsumeDate"));
			
			bagsinfo.setReservedField1(jo.getString("ReservedField1"));
			bagsinfo.setReservedField2(jo.getString("ReservedField2"));

			bagsinfo.setReturnMsg(jo.getString("returnMsg"));
		} else if (type == 2) {
			try {
				JSONTokener jsonParser = new JSONTokener(strResult);
				JSONObject jsonObject = null;
				try {
					jsonObject = (JSONObject) jsonParser.nextValue();
				} catch (JSONException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				ReturnAccountInfo accountinfo = new ReturnAccountInfo();
				accountinfo.setCardID(jsonObject.getInt("CardID"));
				accountinfo.setOpenCardDate(jsonObject
						.getString("OpenCardDate"));
				accountinfo.setUserNo(jsonObject.getString("UserNo"));
				accountinfo.setUserName(jsonObject.getString("UserName"));
				accountinfo.setDeptName(jsonObject.getString("DeptName"));
				accountinfo.setCardStatusName(jsonObject
						.getString("CardStatusName"));

				accountinfo.setCardStartDate(jsonObject
						.getString("CardStartDate"));
				accountinfo.setCardEndDate(jsonObject.getString("CardEndDate"));
				accountinfo.setReservedField1(jsonObject
						.getString("ReservedField1"));

				accountinfo.setReservedField2(jsonObject
						.getString("ReservedField2"));
				// accountinfo.setreturnMsg(jsonObject.getString("returnMsg"));
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}
}
